import {apiRequest} from './client'
import type {
    CatalogSaveVo,
    CatalogVo,
    Column,
    Database,
    DataSourceTreeNode,
    Dimension,
    Table,
} from '../../types/source'

const service = 'SOURCE'

export const sourceApi = {
  // 数据源查询相关接口
  getTree: (displayServer: boolean, displayTable: boolean) =>
    apiRequest<DataSourceTreeNode[]>({
      service,
      method: 'POST',
      path: '/source/tree',
      params: { displayServer, displayTable },
    }),

  getCatalogs: () =>
    apiRequest<CatalogVo[]>({ service, method: 'POST', path: '/source/catalogs' }),

  getCatalog: (catalogName: string) =>
    apiRequest<CatalogVo>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}`,
    }),

  getDatabases: (catalogName: string) =>
    apiRequest<Database[]>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/databases`,
    }),

  getEnabledDatabases: (catalogName: string) =>
    apiRequest<Database[]>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/databases/enabled`,
    }),

  getDatabase: (catalogName: string, databaseName: string) =>
    apiRequest<Database>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}`,
    }),

  getTables: (catalogName: string, databaseName: string) =>
    apiRequest<Table[]>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}/tables`,
    }),

  getEnabledTables: (catalogName: string, databaseName: string) =>
    apiRequest<Table[]>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}/tables/enabled`,
    }),

  getTable: (catalogName: string, databaseName: string, tableName: string) =>
    apiRequest<Table>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}/table/${encodeURIComponent(tableName)}`,
    }),

  getColumns: (catalogName: string, databaseName: string, tableName: string) =>
    apiRequest<Column[]>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}/table/${encodeURIComponent(tableName)}/columns`,
    }),

  getDimensions: (catalogName: string, databaseName: string, tableName: string) =>
    apiRequest<Dimension[]>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}/table/${encodeURIComponent(tableName)}/dimensions`,
    }),

  // 连接测试相关接口
  testServer: (config: CatalogSaveVo) =>
    apiRequest<boolean>({ service, method: 'POST', path: '/source/catalog/test/server', body: config }),

  testJdbc: (config: CatalogSaveVo) =>
    apiRequest<boolean>({ service, method: 'POST', path: '/source/catalog/test/jdbc', body: config }),

  testDoris: (config: CatalogSaveVo) =>
    apiRequest<boolean>({ service, method: 'POST', path: '/source/catalog/test/doris', body: config }),

  testCatalogServer: (catalogName: string) =>
    apiRequest<boolean>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/test/server`,
    }),

  testCatalogJdbc: (catalogName: string) =>
    apiRequest<boolean>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/test/jdbc`,
    }),

  testCatalogDoris: (catalogName: string) =>
    apiRequest<boolean>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/test/doris`,
    }),

  // 数据源管理相关接口
  addCatalog: (payload: CatalogSaveVo) =>
    apiRequest<void>({ service, method: 'POST', path: '/source/catalog/add', body: payload }),

  saveCatalog: (payload: CatalogSaveVo) =>
    apiRequest<void>({ service, method: 'POST', path: '/source/catalog/save', body: payload }),

  // 同步管理相关接口
  syncCatalog: (catalogName: string) =>
    apiRequest<void>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/sync`,
    }),

  syncDatabase: (catalogName: string, databaseName: string) =>
    apiRequest<void>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}/sync`,
    }),

  // 脚本生成相关接口
  generateDorisScript: (catalogName: string, databaseName: string, tableName: string) =>
    apiRequest<string>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}/table/${encodeURIComponent(tableName)}/script/doris`,
    }),

  // 状态管理相关接口
  setCatalogEnabled: (catalogName: string, enabled: boolean) =>
    apiRequest<void>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/enabled`,
      params: { enabled },
    }),

  setDatabaseEnabled: (catalogName: string, databaseName: string, enabled: boolean) =>
    apiRequest<void>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}/enabled`,
      params: { enabled },
    }),

  setTableEnabled: (catalogName: string, databaseName: string, tableName: string, enabled: boolean) =>
    apiRequest<void>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}/table/${encodeURIComponent(tableName)}/enabled`,
      params: { enabled },
    }),

  updateCatalogSystemCode: (catalogName: string, systemCode: string) =>
    apiRequest<void>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/systemCode`,
      params: { systemCode },
    }),

  updateDatabaseSystemCode: (catalogName: string, databaseName: string, systemCode: string) =>
    apiRequest<void>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}/systemCode`,
      params: { systemCode },
    }),

  updateTableSystemCode: (catalogName: string, databaseName: string, tableName: string, systemCode: string) =>
    apiRequest<void>({
      service,
      method: 'POST',
      path: `/source/catalog/${encodeURIComponent(catalogName)}/database/${encodeURIComponent(databaseName)}/table/${encodeURIComponent(tableName)}/systemCode`,
      params: { systemCode },
    }),
}